<?php
/**
 * Created by PhpStorm.
 * User: liuxiu
 * Date: 2021/12/16
 * Time: 15:40
 */

namespace business\procurement\services;


use business\elasticsearch\models\SubsBiddingModel;
use business\procurement\repositories\BiddingAnnouncementRepository;
use common\utils\DateTimeUtils;

class SubsBiddingService
{

    private $isSaveAll = false;

    /* @var  $defaultPageSize int  默认分页值 */
    private $defaultPageSize = 200;

    public function fullIndex($isCreateIndex)
    {
        if ($isCreateIndex == 1) {
            SubsBiddingModel::createIndex();
        }
        $this->updateIndex();
    }

    public function increaseIndex()
    {
        date_default_timezone_set('Asia/Shanghai');

        $createTime = $this->getIncreaseMaxTime();
        $condition  = [
            '>=', 'updated_time', $createTime
        ];
        $this->updateIndex($condition);
    }

    private function updateIndex($condition = [])
    {
        $biddingAnnouRepository = new BiddingAnnouncementRepository();
        $biddingCount           = $biddingAnnouRepository->getBiddingCount($condition);
        echo 'bidding index count:' . $biddingCount . PHP_EOL;
        if (empty($biddingCount)) {
            return true;
        }
        $pageCount = intval(ceil($biddingCount / $this->defaultPageSize));
        echo 'bidding index pageIndex:' . $pageCount . PHP_EOL;
        $fields = ['uid', 'title', 'intro', 'province_code', 'province_name', 'source_type', 'release_time', 'stage', 'is_deleted'];

        for ($i = 1; $i <= $pageCount; $i++) {
            $items = $biddingAnnouRepository->getBidding($condition, $fields, $i, $this->defaultPageSize);
            $this->syncEs($items);
            echo 'cycle to ' . $i . PHP_EOL;
        }
    }

    private function syncEs($list)
    {
        if (empty($list)) {
            return;
        }
        $now = date('Y-m-d H:i:s');
        foreach ($list as $key => $item) {
            try {
                $item['uid']         = intval($item['uid']);
                $item['source_type'] = intval($item['source_type']);
                $item['stage']       = intval($item['stage']);
                $item['release_time'] = $item['release_time'] != '0000-00-00 00:00:00' ? $item['release_time'] :  0;
                $biddingModel        = SubsBiddingModel::findOne(strval($item['uid']));
                if ($item['is_deleted'] == 1 && !empty($biddingModel)) {
                    //todo 删除索引
                    $biddingModel->delete();
                    continue;
                }
                if (empty($biddingModel)) {
                    $biddingModel             = new SubsBiddingModel();
                    $biddingModel->primaryKey = $item['uid'];
                }
                $biddingModel->setAttributes($item, false);
                $biddingModel->indexCreatedTime = $now;
                $rs                             = $biddingModel->save();
                if (!$rs) {
                    echo 'update es_subsbiding failed! uid:' . $item['uid'] . "\n";
                    \Yii::error('update es_subsbiding failed! uid:' . $item['uid']);
                }
            } catch (\Exception $ex) {
                echo $ex->getMessage() . PHP_EOL;
                \Yii::error('update es_subsbiding Exception! uid:' . $item['uid']. ".\t".$ex->getTraceAsString());
            }
        }
    }

    private function getIncreaseMaxTime()
    {
        return date('Y-m-d H:i:s', strtotime('-3 hours'));
    }
}